/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author Breno Leite
 */
public class FileUtils {

    /**
     * Reads the content of a file.
     *
     * @param file                              the file which will has its
     *                                          content readed
     * @return                                  the content of the given file
     * @throws java.io.FileNotFoundException    if the given file does not
     *                                          exists
     * @throws java.io.IOException              if some problem occured while
     *                                          reading the file
     */
    public static String readFile(File file) throws FileNotFoundException, IOException {
        StringBuffer temp = new StringBuffer("");
        String line = "";

        BufferedReader bufferedReader =
                new BufferedReader(new FileReader(file));
        while ((line = bufferedReader.readLine()) != null) {
            temp.append(line);
        }
        return temp.toString();
    }

    public static byte[] readFileBytes(File file)
            throws NullPointerException, IllegalArgumentException, IOException {

        if (file == null) {
            throw new NullPointerException("Can not read the bytes fom a null object.");
        }

        if (file.isDirectory()) {
            throw new IllegalArgumentException("Can read byte just from files.");
        }

        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] content = readBytes(fileInputStream);
        fileInputStream.close();
        return content;
    }

    public static String extractSimpleNameFromFileName(String fileName) {
        try {
            return fileName.substring(0, fileName.indexOf("."));
        } catch (Exception exception) {
            return null;
        }
    }

    public static String extractExtensionFromFileName(String fileName) {
        try {
            return fileName.substring(fileName.indexOf(".") + 1);
        } catch (Exception exception) {
            return null;
        }
    }

    /**
     * Extracts the bytes from a input stream.
     *
     * @param inputStream   the target input stream
     * @return              the file´s bytes
     * @throws IOException  if some error occur while reading the bytes from the
     *                      stream
     */
    public static byte[] readBytes(InputStream inputStream)
            throws IOException {

        byte[] buffer = new byte[1024];
        int returnValue = -1;
        ByteArrayOutputStream outputStream =
                new ByteArrayOutputStream(buffer.length);

        returnValue = inputStream.read(buffer);

        while (-1 != returnValue) {
            outputStream.write(buffer, 0, returnValue);
            returnValue = inputStream.read(buffer);
        }

        outputStream.close();
        inputStream.close();

        return outputStream.toByteArray();
    }
}